package leetcode11_20;

import org.junit.Test;

/**
 *  罗马数字转整数
 * @author shhjiang_001
 * @create 2020-03-09 16:36
 */
public class Solution13 {
    public int romanToInt(String s) {
        int res = 0;
        for (int i = 0; i < s.length(); i++){
            int ans = getValue(s.charAt(i));
            if (i < (s.length()-1) && getValue(s.charAt(i))<getValue(s.charAt(i+1))){
                ans*=-1;
            }
            res = res + ans;
        }
        return res;

    }
    private int getValue(char ch){
        switch (ch){
            case 'I':return 1;
            case 'V':return 5;
            case 'X':return 10;
            case 'L':return 50;
            case 'C':return 100;
            case 'D':return 500;
            case 'M':return 1000;
            default:return 0;
        }
    }
    @Test
    public void test(){
        Solution13 solution13 = new Solution13();
        int i = solution13.romanToInt("IV");
        System.out.println(i);
    }
}
